package com.edu.web.servlet;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.edu.entiy.Book;
import com.edu.entiy.History;
import com.edu.entiy.User;
import com.edu.service.BookService;
import com.edu.service.HistoryService;
import com.edu.service.UserService;
import com.edu.service.impl.BookServiceImpl;
import com.edu.service.impl.HistoryServiceImpl;
import com.edu.service.impl.UserServiceImpl;
import com.edu.utils.PageTool;
import com.edu.utils.PaginationUtils;

/**
 * Servlet implementation class HistoryServlet
 */
@WebServlet("/historyServlet/*")
public class HistoryServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private HistoryService historyService = new HistoryServiceImpl();
	private BookService bookService = new BookServiceImpl();
	private UserService userService = new UserServiceImpl();
    public HistoryServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    public void backPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	//获取数据
    	HttpSession session = request.getSession();
    	//获取当前用户的id
    	User user = (User)session.getAttribute("user");
    	Integer status = user.getStatus();
    	//获取传入的数据
        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");
        //如果用户当前身份是用户,则只能查询到自己借阅过的书籍
        PageTool<History> pageTool = null;
        if(status == 1) {
        	pageTool =  historyService.selectByPage(_currentPage, _pageSize,user.getId(),1);
        }else {
        	pageTool =  historyService.selectByPage(_currentPage, _pageSize,null,1);
        }
    	//生成前端组件分页按钮
    	String pageResult =  PaginationUtils.getPagation(pageTool.getTotalCount(), pageTool.getCurrentPage(), pageTool.getPageSize(), "/historyServlet/backPage");
    	request.setAttribute("pageResult", pageResult);
    	request.setAttribute("historyList", pageTool.getRows());
    	if(status == 2) {
    		request.getRequestDispatcher("/admin/admin_history.jsp").forward(request, response);
    	}else {
    		request.getRequestDispatcher("/user/history.jsp").forward(request, response);
    	}
    }
    public void page(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	//获取数据
    	HttpSession session = request.getSession();
    	//获取当前用户的id
    	User user = (User)session.getAttribute("user");
    	Integer status = user.getStatus();
    	//获取传入的数据
        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");
        //如果用户当前身份是用户,则只能查询到自己借阅的书籍
        PageTool<History> pageTool = null;
        if(status == 1) {
        	System.out.println("我是用户");
        	pageTool =  historyService.selectByPage(_currentPage, _pageSize,user.getId(),0);
        }else {
        	System.out.println("我是管理员");
        	pageTool =  historyService.selectByPage(_currentPage, _pageSize,null,0);
        }
    	//生成前端组件分页按钮
    	String pageResult =  PaginationUtils.getPagation(pageTool.getTotalCount(), pageTool.getCurrentPage(), pageTool.getPageSize(), "/historyServlet/page");
    	request.setAttribute("pageResult", pageResult);
    	request.setAttribute("historyList", pageTool.getRows());
    	System.out.println(pageTool.getRows());
    	if(status == 2) {
    		request.getRequestDispatcher("/admin/admin_borrow.jsp").forward(request, response);
    	}else {
    		request.getRequestDispatcher("/user/borrow.jsp").forward(request, response);
    	}
    	
    }
    //还书
  	public void returnBook (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  		//获取被还图书的id
  		String _historyId = request.getParameter("historyId");
  		int historyId = Integer.parseInt(_historyId);
  		System.out.println(historyId);
  		//获取用户对象
  		HttpSession session = request.getSession();
  		User loginUser =(User) session.getAttribute("user");
  		System.out.println(loginUser);
  		History history =  historyService.selectHistory(historyId);
  		System.out.println(history);
  		User user = userService.selectUserById(history.getUserId());
  		//根据书籍id查找到书籍
  		Book book = bookService.selectBookById(history.getBookId());
 
  		boolean result = historyService.returnBook(book, user, history);
  		if(result) {
  			//还书成功,我们要更新session中的信息
  			user = userService.selectUserById(loginUser.getId());
  			System.out.println(user);
  			session.setAttribute("user", loginUser);
  			request.getRequestDispatcher("/historyServlet/backPage").forward(request, response);
  		}else {
  			System.out.println("还书失败");
  		}
  	}
  	//延期
  	public void addTime(HttpServletRequest request, HttpServletResponse response) throws ParseException, ServletException, IOException {
  		String endTime = request.getParameter("endTime");
  		String _historyId = request.getParameter("historyId");
  		int historyId = Integer.parseInt(_historyId);
  		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = simpleDateFormat.parse(endTime);
		Timestamp timestamp = new Timestamp(date.getTime());
		historyService.updateEndTime(timestamp, historyId);
		request.getRequestDispatcher("/historyServlet/page").forward(request, response);
  	}
}
